/*
 ============================================================================
 Name		: Util.cpp
 Author	  : samuelmo
 Version	 : 1.0
 Copyright   : 
 Description : CUtil implementation
 ============================================================================
 */

#include "StrUtil.h"
#include <eikenv.h>
#include <coeutils.h>
#include <uri8.h>
#include <commdb.h>				
#include <commdbconnpref.h>	

HBufC16* StrUtil::ConvertToUnicode(const TDesC8& text, TUint aCharset)
{
	//CcnvCharacterSetConverter API来转换 
	CCnvCharacterSetConverter* converter = CCnvCharacterSetConverter::NewLC();
	if (converter->PrepareToConvertToOrFromL(aCharset, CEikonEnv::Static()->FsSession()) != CCnvCharacterSetConverter::EAvailable)
		User::Leave(KErrNotSupported);

	TInt state = CCnvCharacterSetConverter::KStateDefault;

	HBufC16* iInfoText = HBufC16::NewL(text.Length());
	TPtr16 ptr = iInfoText->Des();
	if (CCnvCharacterSetConverter::EErrorIllFormedInput == converter->ConvertToUnicode(ptr, text, state))
		User::Leave(KErrArgument); //Leave if error in conversion. 
	CleanupStack::PopAndDestroy(); //clean for converter 

	return iInfoText;
}

HBufC8* StrUtil::ConvertFromUnicode(const TDesC& text, TUint aPrimCharacter)
{
	//CcnvCharacterSetConverter API来转换 
	CCnvCharacterSetConverter* converter = CCnvCharacterSetConverter::NewLC();
	if (converter->PrepareToConvertToOrFromL(aPrimCharacter, CEikonEnv::Static()->FsSession()) != CCnvCharacterSetConverter::EAvailable)
		User::Leave(KErrNotSupported);

	TInt state = CCnvCharacterSetConverter::KStateDefault;

	HBufC8* iInfoText = HBufC8::NewL(text.Length() * 3);
	TPtr8 ptr = iInfoText->Des();
	if (CCnvCharacterSetConverter::EErrorIllFormedInput == converter->ConvertFromUnicode(ptr, text, state))
		User::Leave(KErrArgument); //Leave if error in conversion. 
	CleanupStack::PopAndDestroy(); //clean for converter 
	return iInfoText;
}

TTime StrUtil::ParseTime(const TDesC8& aTime)
{
	TPtrC8 year(aTime.Mid(12, 4));
	TPtrC8 month(aTime.Mid(8, 3));
	TPtrC8 day(aTime.Mid(5, 2));
	TPtrC8 hour(aTime.Mid(17, 2));
	TPtrC8 minute(aTime.Mid(20, 2));
	TPtrC8 second(aTime.Mid(23, 2));

	TDateTime time;
	//年
	{
		TInt year2;
		TLex8 lex(year);
		lex.Val(year2);
		time.SetYear(year2);
	}
	//月
	{
		if (month.Compare(_L8("Jan")) == 0)
		{
			time.SetMonth(EJanuary);
		}
		else if (month.Compare(_L8("Feb")) == 0)
		{
			time.SetMonth(EFebruary);
		}
		else if (month.Compare(_L8("Mar")) == 0)
		{
			time.SetMonth(EMarch);
		}
		else if (month.Compare(_L8("Apr")) == 0)
		{
			time.SetMonth(EApril);
		}
		else if (month.Compare(_L8("May")) == 0)
		{
			time.SetMonth(EMay);
		}
		else if (month.Compare(_L8("Jun")) == 0)
		{
			time.SetMonth(EJune);
		}
		else if (month.Compare(_L8("Jul")) == 0)
		{
			time.SetMonth(EJuly);
		}
		else if (month.Compare(_L8("Aug")) == 0)
		{
			time.SetMonth(EAugust);
		}
		else if (month.Compare(_L8("Sep")) == 0)
		{
			time.SetMonth(ESeptember);
		}
		else if (month.Compare(_L8("Oct")) == 0)
		{
			time.SetMonth(EOctober);
		}
		else if (month.Compare(_L8("Nov")) == 0)
		{
			time.SetMonth(ENovember);
		}
		else if (month.Compare(_L8("Dec")) == 0)
		{
			time.SetMonth(EDecember);
		}
	}
	//日
	{
		TInt day2;
		TLex8 lex(day);
		lex.Val(day2);
		time.SetDay(day2);
	}
	//时，分，秒
	{
		TInt hour2;
		TLex8 lex(hour);
		lex.Val(hour2);
		time.SetHour(hour2);
	}
	{
		TInt minute2;
		TLex8 lex(minute);
		lex.Val(minute2);
		time.SetMinute(minute2);
	}
	{
		TInt second2;
		TLex8 lex(second);
		lex.Val(second2);
		time.SetSecond(second2);
	}
	return TTime(time);
}

void StrUtil::GetIAP(RArray<TIap>& iaps)
{
	iaps.Reset();

	CCommsDatabase* commDb = CCommsDatabase::NewL(EDatabaseTypeIAP);
	CleanupStack::PushL(commDb);
	CCommsDbTableView* commView = commDb->OpenTableLC(TPtrC(IAP));
	if (commView->GotoFirstRecord() == KErrNone)
	{
		do
		{
			TIap iap;
			commView->ReadUintL(TPtrC(COMMDB_ID), iap.iId);
			commView->ReadTextL(TPtrC(COMMDB_NAME), iap.iName);

			User::LeaveIfError(iaps.Append(iap));
		} while (commView->GotoNextRecord() == KErrNone);
	}
	CleanupStack::PopAndDestroy(/*commView*/);
	CleanupStack::PopAndDestroy(/*commDb*/);
}

void StrUtil::Log(const TDesC& aLog)
{
	HBufC8* tmp = ConvertFromUnicode(aLog, KCharacterSetIdentifierGb2312);
	Log(*tmp);
	delete tmp;
}

void StrUtil::Log(const TDesC8& aLog)
{
	TFileName path;
#ifdef __WINS__
	path.Copy(_L("C:\\log.txt"));
#else
	path.Copy(_L("E:\\log.txt"));
#endif

	RFs fs;
	User::LeaveIfError(fs.Connect());
	RFile file;

	if (ConeUtils::FileExists(path))
	{
		file.Open(fs, path, EFileShareAny | EFileWrite);
		TInt pos = 0;
		file.Seek(ESeekEnd, pos);
	}
	else
	{
		file.Create(fs, path, EFileShareAny | EFileWrite);
	}

	file.Write(aLog);
	file.Write(_L8("\r\n"));
	file.Close();
	fs.Close();
}
